//
//  RecipesViewController.m
//  SVN_Tuan_Thuy
//
//  Created by HOCVIEN on 4/21/15.
//  Copyright (c) 2015 Module4. All rights reserved.
//

#import "RecipesViewController.h"
#import "RecipesTableViewCell.h"
#import "RecipesDetailViewController.h"

@interface RecipesViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *foodList;
@property (nonatomic) UIActivityIndicatorView *waiting;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RecipesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [AppUtils themeColor];
    [self waitingSetting];
    [self reloadData];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cellId";
    RecipesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        [tableView registerClass:[RecipesTableViewCell class] forCellReuseIdentifier:cellID];
        cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }
    Food *foodDetail = [self.foodList objectAtIndex:indexPath.row];
    UIImage *image = [[SAMCache sharedCache] imageForKey:foodDetail.foodID];
    if (image) {
        cell.imgRecipes.image = image;
    } else {
        cell.imgRecipes.image = [UIImage imageNamed:@"thumbnail-default.jpg"];
    }

    cell.url = foodDetail.foodImageURL;

    dispatch_async(dispatch_queue_create("loadimage", nil), ^{
            NSURL *url = [NSURL URLWithString:foodDetail.foodImageURL];
            NSData *data = [NSData dataWithContentsOfURL:url];
    
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([cell.url isEqualToString:url.absoluteString]) {
                    cell.imgRecipes.image =[UIImage imageWithData:data];
                    [[SAMCache sharedCache] setImage:[UIImage imageWithData:data] forKey:foodDetail.foodID];
                }
            });
    });
    cell.lblRecipeName.text = foodDetail.foodTitle;
    cell.lblRecipePublicer.text = [NSString stringWithFormat:@"%@", foodDetail.foodPublisher];
    int star = 0;
    if ([foodDetail.foodSocialRank integerValue] >= 71) {
        star = 3;
    } else if ([foodDetail.foodSocialRank integerValue] >= 40) {
        star = 2;
    } else if ([foodDetail.foodSocialRank integerValue] >= 11) {
        star = 1;
    }
    int x = 0, i = 0;
    while (i < 3) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 15, 15)];
        if (i < star) {
            image.image = [UIImage imageNamed:@"star_full.png"];
        } else {
            image.image = [UIImage imageNamed:@"star_empty.png"];
        }
        [cell.socialRankView addSubview:image];
        x += 15;
        i++;
    }

    return cell;
}
- (void)reloadData {
    [self.waiting startAnimating];
    [[AppUtils defaultAppUtils] loadData:^(NSMutableArray *foodList, NSError *error) {
        self.foodList = foodList;
        [self.tableView reloadData];
        [self.waiting stopAnimating];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.foodList count];
}
- (IBAction)onTouchReloadData:(UIBarButtonItem *)sender {
    [self reloadData];
}
- (void)waitingSetting {
    self.waiting = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.waiting.color = [AppUtils themeFontColor];
    self.waiting.center = self.view.center;
    self.waiting.hidesWhenStopped = true;
    [self.view addSubview:self.waiting];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    RecipesDetailViewController *recipeDetailView = (RecipesDetailViewController *)segue.destinationViewController;
    recipeDetailView.food = [self.foodList objectAtIndex:self.tableView.indexPathForSelectedRow.row];
}
#pragma Iad Delegate
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    [banner setAlpha:1.0f];
    [UIView commitAnimations];
    
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    [banner setAlpha:0];
    [UIView commitAnimations];
    
}
@end
